package no.sachmuld.methodcombination.api.reflection.component;

import no.sachmuld.methodcombination.api.BehaviourChain;
import no.sachmuld.methodcombination.api.CrosscuttingBehaviour;
import no.sachmuld.methodcombination.api.MethodInvocation;

import java.lang.reflect.Method;

/**
 *
 */
public class CrosscuttingBeforeMethodCombination<B extends CrosscuttingBehaviour> extends DelegatingMethodCombination<B, Void> {

    @Override
    public CrosscuttingBeforeMethodCombination<B> build(Class behaviourClass, final Method targetMethod) {

        executedBy(new AbstractMethodCombination<B, Void>() {
            @Override
            public Void execute(final Object targetObject, BehaviourChain<? extends B> behaviourChain, Object... parameters) {
                MethodInvocation auxilliaryMethodInvocation = new AuxiliaryMethodInvocation(targetObject, targetMethod, parameters);

                for (B behaviour : behaviourChain.bottomUp()) {
                    behaviour.before(auxilliaryMethodInvocation);
                }
                return null;
            }
        });

        return this;
    }

}
